package com.cater.examhelper.db;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.cater.examhelper.DatabaseHelper;
import com.cater.examhelper.model.CollectRecord;
import com.cater.examhelper.util.EncryptClazz;

public class ChapterTricksDbAdapter
{
	public static final String TABLE_NAME = "chapter";
	public static final String ID = "id";
	public static final String COURSE_ID = "courseid";
	public static final String CHAPTER_ID = "chapterid";
	public static final String CHAPTER_NAME = "chaptername";
	public static final String IS_PASSED = "ispassed";
	public static final String ITEM1 = "item1";
	public static final String ITEM2 = "item2";
	public static final String CHAPTER_NUMBER = "chapternumber";

	private DatabaseHelper dbHelper;
	private SQLiteDatabase mdb;
	private Context ctx;

	public ChapterTricksDbAdapter(Context ctx)
	{
		this.ctx = ctx;
	}

	public ChapterTricksDbAdapter open()
	{
		dbHelper = new DatabaseHelper(ctx);
		mdb = dbHelper.getWritableDatabase();
		return this;
	}

	public void close()
	{
		mdb.close();
		dbHelper.close();
	}

	public List<Map<String, Object>> getChaptersList(int course)
	{
		Cursor cursor = null;
		cursor = mdb.rawQuery("select * from " + TABLE_NAME + " where courseid = " + course, null);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		while (cursor.moveToNext())
		{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(ID, String.valueOf(cursor.getInt(0)));
			map.put(COURSE_ID, cursor.getString(cursor.getColumnIndex(COURSE_ID)));
			map.put(CHAPTER_ID, cursor.getString(cursor.getColumnIndex(CHAPTER_ID)));
			String name = null;
			try
			{
				name = new String(cursor.getBlob(cursor.getColumnIndex(CHAPTER_NAME)), "GBK");
			}
			catch (UnsupportedEncodingException e)
			{
				Log.d("chapterName", e.getMessage());
			}
			map.put(CHAPTER_NAME, name);
			map.put(IS_PASSED, cursor.getInt(cursor.getColumnIndex(IS_PASSED)));
			map.put(ITEM1, cursor.getString(cursor.getColumnIndex(ITEM1)));
			map.put(ITEM2, cursor.getString(cursor.getColumnIndex(ITEM2)));
			map.put(CHAPTER_NUMBER, cursor.getInt(cursor.getColumnIndex(CHAPTER_NUMBER)));
			list.add(map);
		}
		cursor.close();
		return list;
	}

	public List<Map<String, Object>> getChapterQuestions(String i, int course)
	{
		Cursor c = mdb.rawQuery("select * from  question where chapter = ? and course = ?", new String[] { i, String.valueOf(course) });
		List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
		String masterPassword = "c";
		while (c.moveToNext())
		{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", c.getInt(0));
			map.put("course", c.getString(1));
			map.put("chapter", c.getInt(2));
			map.put("questionid", c.getInt(3));

			String question = null;
			String option1 = null;
			String option2 = null;
			String option3 = null;
			String option4 = null;
			String option5 = null;
			String explain = null;
			String item1 = null;
			try
			{
				question = new String(c.getBlob(c.getColumnIndex("question")), "GBK");
				question = EncryptClazz.decrypt(masterPassword, question);
				option1 = new String(c.getBlob(c.getColumnIndex("option1")), "GBK");
				option1 = EncryptClazz.decrypt(masterPassword, option1);
				option2 = new String(c.getBlob(c.getColumnIndex("option2")), "GBK");
				option2 = EncryptClazz.decrypt(masterPassword, option2);
				option3 = new String(c.getBlob(c.getColumnIndex("option3")), "GBK");
				option3 = EncryptClazz.decrypt(masterPassword, option3);
				option4 = new String(c.getBlob(c.getColumnIndex("option4")), "GBK");
				option4 = EncryptClazz.decrypt(masterPassword, option4);
				option5 = new String(c.getBlob(c.getColumnIndex("option5")), "GBK");
				option5 = EncryptClazz.decrypt(masterPassword, option5);
				explain = new String(c.getBlob(c.getColumnIndex("explain")), "GBK");
				explain = EncryptClazz.decrypt(masterPassword, explain);
				map.put("explain", explain);
				item1 = new String(c.getBlob(c.getColumnIndex("item1")), "GBK");
				item1 = EncryptClazz.decrypt(masterPassword, item1);
				map.put("item1", item1);
			}
			catch (Exception e2)
			{
				e2.printStackTrace();
			}
			try
			{
				map.put("question", question);
				map.put("option1", option1);
				map.put("option2", option2);
				map.put("option3", option3);
				map.put("option4", option4);
				map.put("option5", option5);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			map.put("answer", c.getString(10));
			map.put("questiontype", c.getInt(12));
			map.put("questionclass", c.getInt(13));
			map.put("isexampaper", c.getInt(14));
			map.put("item2", c.getString(16));
			map.put("item3", c.getString(17));
			listData.add(map);
		}
		c.close();
		return listData;
	}

	public void collectRecord(CollectRecord record)
	{
		mdb.execSQL("insert into collectrecord(questionid,chapterid,courseid) values(?,?,?)", new Object[] { record.getQuestionId(), record.getChapterId(),record.getCourseId() });
	}

	public boolean findRecord(CollectRecord record)
	{
		Cursor c = mdb.rawQuery("select questionid from collectrecord where questionid = ?", new String[] { record.getQuestionId() + "" });
		if (c.getCount() > 0)
		{
			return true;
		}
		c.close();
		return false;
	}

	public void wrongRecord(CollectRecord record)
	{
		mdb.execSQL("insert into wrongrecord(questionid,chapterid,courseid) values(?,?,?)", new Object[] { record.getQuestionId(), record.getChapterId(),record.getCourseId() });
	}

	public boolean updateChapterRecord(String chapter, int score, int course)
	{
		ContentValues values = new ContentValues();
		values.put("ispassed", score);
		int number = mdb.update(TABLE_NAME, values, "chapterid = ? and courseid= ?", new String[] { chapter, String.valueOf(course) });
		if (number < 0)
		{
			return false;
		}
		return true;
	}
}
